package com.springboot.cloud.demos.producer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.demos.entity.param.UserPositionQueryParam;
import com.springboot.cloud.demos.entity.po.UserPosition;
import com.springboot.cloud.demos.producer.dao.UserPositionMapper;
import com.springboot.cloud.demos.producer.service.IUserPositionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@RefreshScope
public class UserPositionServiceImpl extends ServiceImpl<UserPositionMapper, UserPosition> implements IUserPositionService {
    @Value("${producer.userPosition:123}")
    private String value;

    @Override
    @CacheEvict(value = "userPosition",allEntries=true)
    public boolean add(UserPosition userPosition) {
        return this.save(userPosition);
    }

    @Override
    @CacheEvict(value = "userPosition", key = "#root.targetClass+'-'+#id")
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @CacheEvict(value = "userPosition", key = "#root.targetClass+'-'+#userPosition.id")
    public boolean update(UserPosition userPosition) {
        return this.updateById(userPosition);
    }

    @Override
    @Cacheable(value = "userPosition", key = "#root.targetClass+'-'+#id")
    public UserPosition get(String id) {
        log.info("value:{}", value);
        return this.getById(id);
    }

    @Override
//    @Cacheable(value = "userPosition", key = "#root.targetClass+'-user-'+#param.userId")
    public List<UserPosition> query(UserPositionQueryParam param) {
        QueryWrapper<UserPosition> queryWrapper = param.build();
        queryWrapper.eq("user_id", param.getUserId());
        queryWrapper.orderByAsc("created_time");
        return this.list(queryWrapper);
    }
}
